package com.ld.xuanKe.service.impl;

import com.ld.xuanKe.dao.TeachingClassDAO;
import com.ld.xuanKe.entity.Course;
import com.ld.xuanKe.entity.TeachingClass;
import com.ld.xuanKe.service.CourseService;
import com.ld.xuanKe.service.TeachingClassService;
import com.ld.xuanKe.service.XsTeachingClassService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName xuanKe
 * @ClassName TeachingClassServiceImpl
 * @Author 刘邓
 * @Date 2023-12-20 21:49
 */
@Service
public class TeachingClassServiceImpl implements TeachingClassService {
	@Resource
	private XsTeachingClassService xsTeachingClassService;
	@Resource
	private TeachingClassDAO teachingClassDAO;
	@Resource
	private CourseService courseService;

	public int add(TeachingClass teachingClass) {
		int insert;
		TeachingClass entity = teachingClassDAO.findOne("from TeachingClass T where T.courseId = ?", new Object[]{teachingClass.getCourseId()});
		if(entity == null){
			 insert = teachingClassDAO.insert(teachingClass);
		}else {
			insert = 0;
		}
		return insert;
	}

	public TeachingClass querySingleByCourseId(Integer courseId) {
		return teachingClassDAO.findOne("from TeachingClass T where T.courseId = ?",new Object[]{courseId});
	}

	public int addList() {
		List<Course> courses = courseService.getValidCoursesAfterSelect();
		int add = 0;
		for (Course course : courses) {
			TeachingClass teachingClass = new TeachingClass();
			teachingClass.setName(course.getName() + course.getId() + "班");
			teachingClass.setCourseId(course.getId());
			add +=  this.add(teachingClass);
		}
//		生成学生和教学班的联系
		int i = xsTeachingClassService.addList();
		if(i == 0 || add == 0){
			System.out.println(66);
			return 0;
		}else {
			return 1;
		}
	}

	public List<TeachingClass> list() {
		return teachingClassDAO.find("from TeachingClass",new Object[]{});
	}

	public List<TeachingClass> listByCourseId(Integer courseId) {
		return teachingClassDAO.find("from TeachingClass T where T.courseId = ?",new Object[]{courseId});
	}
}
